package com.bangdream.michelia.view.fragment.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.entity.CoursesTypeBean;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.tool.FullyLinearLayoutManager;
import com.bangdream.michelia.view.activity.currency.MainActivity;
import com.bangdream.michelia.view.activity.currency.OnMainActivityOperationListener;
import com.bangdream.michelia.view.adapter.SlideAdapter;
import com.bangdream.michelia.view.adapter.TaskExaminationAdapter;
import com.bangdream.michelia.view.fragment.currency.BaseFragment;

import java.util.List;

/**
 * @date on 2018/6/20 0:01
 * @email 909732184@qq.com
 * 右边的侧边栏
 */
public class SlideRightFragment extends BaseFragment{
    private View view;
    private View statusBarLayout;
    private RecyclerView recyclerView;
    private FullyLinearLayoutManager linearLayoutManager;
    private XRefreshView refreshView;
    private SlideAdapter adapter;
    private List<CoursesTypeBean> typeBeans;
    private OnMainActivityOperationListener onMainActivityOperationListener;

    public OnMainActivityOperationListener getOnMainActivityOperationListener() {
        return onMainActivityOperationListener;
    }

    public void setOnMainActivityOperationListener(OnMainActivityOperationListener onMainActivityOperationListener) {
        this.onMainActivityOperationListener = onMainActivityOperationListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.slide_reight_layout,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        setListener();
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    private void setListener() {
        adapter.setOnSlideClickListener(new SlideAdapter.OnSlideClickListener() {
            @Override
            public void onSlideItemClick(int position) {
                if(onMainActivityOperationListener!=null){
                    onMainActivityOperationListener.onOpenSlide(false);
                    onMainActivityOperationListener.onSlideItemClick(position,typeBeans.get(position));
                }
            }
        });
    }

    private void initView() {

        refreshView = view.findViewById(R.id.refreshView);
        recyclerView = view.findViewById(R.id.recyclerView);
        statusBarLayout = view.findViewById(R.id.statusBarLayout);
        linearLayoutManager = new FullyLinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        ViewGroup.LayoutParams layoutParams = statusBarLayout.getLayoutParams();
        layoutParams.height = MainActivity.statusBarHeight;
        statusBarLayout.setLayoutParams(layoutParams);
        refreshView.setPullRefreshEnable(false);
        refreshView.setPullLoadEnable(false);
        adapter = new SlideAdapter(getActivity());
        recyclerView.setAdapter(adapter);
        typeBeans = dbHelper.queryAll(daoInstancs.getCoursesTypeBeanDao());
        adapter.setData(typeBeans);
        adapter.notifyDataSetChanged();
    }

    public void setData(){
        if(adapter!=null){
            List<CoursesTypeBean> list = dbHelper.queryAll(daoInstancs.getCoursesTypeBeanDao());
            adapter.setData(list);
            adapter.notifyDataSetChanged();
        }
    }
}
